package com.wwp.tbshop.view.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wwp.tbshop.R;
import com.wwp.tbshop.model.bean.CollectListBean;
import com.wwp.tbshop.util.Constant;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Xiaokun on 2017/6/17.
 */

public class CollectAdapter extends BaseAdapter {


    private Context mContext;
    private List<CollectListBean.ProductListBean> mProductList;

    public CollectAdapter(Context context) {
        this.mContext = context;
    }

    public void setDatas(List<CollectListBean.ProductListBean> productList) {
        mProductList = productList;
    }


    @Override
    public int getCount() {
        if (mProductList != null) {
            Log.e("test", "getCount:"+mProductList.size() );
            return mProductList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return mProductList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        CollectViewHolder viewHolder=null;
        if (convertView == null) {
            convertView = View.inflate(mContext,R.layout.item_collect,null);
            viewHolder=new CollectViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (CollectViewHolder) convertView.getTag();
        }
        viewHolder.setData(mProductList.get(position));

        return convertView;
    }


    class CollectViewHolder {
        @Bind(R.id.iv_goods_pic)
        ImageView ivGoodsPic;
        @Bind(R.id.tv_goods_name)
        TextView tvGoodsName;
        @Bind(R.id.tv_goods_price)
        TextView tvGoodsPrice;
        @Bind(R.id.tv_old_price)
        TextView tvOldPrice;

        CollectViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

        public void setData(CollectListBean.ProductListBean productListBean) {
            tvGoodsName.setText(productListBean.getName());
            tvGoodsPrice.setText("惊喜价￥" + productListBean.getPrice());
            tvOldPrice.setText("原价￥" + productListBean.getMarketPrice());
            tvOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            Picasso.with(mContext).load(Constant.HOST + productListBean.getPic()).into(ivGoodsPic);
        }
    }
}
